﻿#if NETSTANDARD2_0 || NETCOREAPP3_0
using Microsoft.Extensions.Logging;
#endif
using System;

namespace Longbow.Logging
{
    /// <summary>
    /// 后台服务管理日志操作类
    /// </summary>
#if NETSTANDARD2_0 || NETCOREAPP3_0
    public class LoggerProvider : ILoggerProvider
#else
    public class LoggerProvider : IDisposable
#endif
    {
        /// <summary>
        /// 
        /// </summary>
        protected Action<string> LogCallback { get; set; }

        /// <summary>
        /// 
        /// </summary>
        protected Func<string, LogLevel, bool> Filter { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logCallback"></param>
        /// <param name="filter"></param>
        public LoggerProvider(Action<string> logCallback, Func<string, LogLevel, bool> filter = null)
        {
            LogCallback = logCallback;
            Filter = filter;
        }

        /// <summary>
        /// 创建 ILogger 实例方法
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public virtual ILogger CreateLogger(string categoryName)
        {
            return new Logger(categoryName, LogCallback, Filter);
        }

        /// <summary>
        /// 创建 ILogger 实例方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual ILogger CreateLogger<T>() => CreateLogger(typeof(T).Name);

        /// <summary>
        /// Dispose
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {

            }
        }

        // This code added to correctly implement the disposable pattern.
        /// <summary>
        /// Dispose 方法
        /// </summary>
        public void Dispose()
        {
            // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
